package com.hj.lib.util;

import cn.hutool.core.io.FileUtil;
import com.alibaba.fastjson.JSON;
import com.hj.lib.entity.AjaxJson;
import com.hj.lib.entity.HjPacket;
import com.hj.lib.entity.Pz;
import com.hj.lib.impl.HjTioHandle;
import com.hj.lib.impl.JavaBrowserImpl;
import com.hj.lib.tio.HjClientAioHandler;
import org.tio.client.ClientChannelContext;
import org.tio.client.ClientGroupContext;
import org.tio.client.ReconnConf;
import org.tio.client.TioClient;
import org.tio.client.intf.ClientAioHandler;
import org.tio.client.intf.ClientAioListener;
import org.tio.core.Node;
import org.tio.core.Tio;

import static com.hj.lib.util.TioConfig.*;

public class HjTioUtil {

	//服务器节点
	public static Node serverNode = new Node(SERVER, TioConfig.PORT);
	//handler, 包括编码、解码、消息处理
	public static ClientAioHandler tioClientHandler = new HjClientAioHandler();
	//事件监听器，可以为null，但建议自己实现该接口，可以参考showcase了解些接口
	public static ClientAioListener aioListener = null;
	//断链后自动连接的，不想自动连接请设为null
	private static ReconnConf reconnConf = new ReconnConf(5000L);
	//一组连接共用的上下文对象
	public static ClientGroupContext clientGroupContext = new ClientGroupContext(tioClientHandler, aioListener, reconnConf);
	public static TioClient tioClient = null;
	public static ClientChannelContext clientChannelContext = null;
	public static HjTioHandle hjTioHandle = null;
	public static String ids;
	/**
	 * 启动程序入口
	 */
	public static void send(AjaxJson ajaxJson) {
		try {
			HjPacket packet = new HjPacket();
			packet.setBody(JSON.toJSONString(ajaxJson).getBytes(HjPacket.CHARSET));
			Tio.send(clientChannelContext, packet);
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	public static void startClient(HjTioHandle hjTioHandle) {
		String pzStr = "";
		try {
			pzStr = FileUtil.readString(JavaBrowserImpl.ipPath, "utf-8");
			if (pzStr != null && !"".equals(pzStr)) {
				Pz sz = JSON.parseObject(pzStr,Pz.class);
				serverNode.setIp(sz.getIp());
				if (sz.getIp() != null && !("").equals(sz.getIp())) {
					SERVER = sz.getIp();
				}
				if (sz.getYwPort() != null && !("").equals(sz.getYwPort())) {
					ywPort = sz.getYwPort();
				}
				if (sz.getCtx() != null && !("").equals(sz.getCtx())) {
					TioConfig.ctx = sz.getCtx();
				}
				if (sz.getFqqm() != null && !("").equals(sz.getFqqm())) {
					HjClientAioHandler.FQQM_CMD = sz.getFqqm();

				}
				if (sz.getTzrh() != null && !("").equals(sz.getTzrh())) {
					HjClientAioHandler.TZRH_CMD = sz.getTzrh();
				}
				home = "https://"+SERVER+":8443"+"/"+ctx+"/hjMeetingController.do?"+view+"&mac="+DevUtil.getMACAddress();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		ids = System.currentTimeMillis() + "";
		HjTioUtil.hjTioHandle = hjTioHandle;
		clientGroupContext.setHeartbeatTimeout(TioConfig.TIMEOUT);
		try {
			tioClient = new TioClient(clientGroupContext);
			clientChannelContext = tioClient.connect(serverNode);
			AjaxJson ajaxJson = new AjaxJson();
			ajaxJson.setCode(AjaxJson.HjTioCode.RegDev);
			ajaxJson.setObj(pzStr);
			HjTioUtil.send(ajaxJson);
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

}
